package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketGoods;
import com.cskaoyan.app.bean.po.MarketGoodsExample;
import com.cskaoyan.app.bean.po.MarketTopic;
import com.cskaoyan.app.bean.po.MarketTopicExample;
import com.cskaoyan.app.bean.vo.AdminTopicReadVO;
import com.cskaoyan.app.mapper.MarketGoodsMapper;
import com.cskaoyan.app.mapper.MarketTopicMapper;
import com.cskaoyan.app.service.AdminTopicService;
import com.cskaoyan.app.util.StringsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    private MarketTopicMapper topicMapper;

    @Autowired
    private MarketGoodsMapper goodsMapper;

    @Override
    public CommonData<MarketTopic> list(BasePageInfo info, String title, String subtitle) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();

        if (!StringsUtil.isEmpty(title)) {
            criteria.andTitleLike(StringsUtil.getConcat(title));
        }
        if (!StringsUtil.isEmpty(subtitle)) {
            criteria.andSubtitleLike(StringsUtil.getConcat(subtitle));
        }

        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(StringsUtil.getOrderClause(info.getSort(), info.getOrder()));

        List<MarketTopic> list = topicMapper.selectByExampleWithBLOBs(example);
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketTopic create(MarketTopic marketTopic) {
        marketTopic.setAddTime(new Date());
        marketTopic.setUpdateTime(new Date());
        topicMapper.insertSelective(marketTopic);
        return topicMapper.selectByPrimaryKey(marketTopic.getId());
    }

    @Override
    public AdminTopicReadVO read(Integer id) {
        MarketTopic topic = topicMapper.selectByPrimaryKey(id);
        Integer[] goods = topic.getGoods();

        List<MarketGoods> goodsList = new ArrayList<>();
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();

        if (goods.length != 0) {
            criteria.andIdIn(Arrays.asList(goods));
            goodsList = goodsMapper.selectByExampleWithBLOBs(example);
        }

        return new AdminTopicReadVO(goodsList, topic);
    }

    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        topicMapper.updateByPrimaryKeySelective(marketTopic);
        return topicMapper.selectByPrimaryKey(marketTopic.getId());
    }

    @Override
    public void delete(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        marketTopic.setDeleted(true);
        topicMapper.updateByPrimaryKeySelective(marketTopic);
    }

    @Override
    public void batchDelete(Map<String, Integer[]> map) {
        Integer[] ids = map.get("ids");

        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();

        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setDeleted(true);

        if (ids.length != 0) {
            criteria.andIdIn(Arrays.asList(ids));
            topicMapper.updateByExampleSelective(marketTopic, example);
        }
    }
}
